package com.example.yichuangyun.utils;

import com.example.yichuangyun.application.MyApplication;
import com.umeng.analytics.MobclickAgent;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class UMengUtil {
    public static void setCountEventId(String eventId) {
        Map<String, String> map = new TreeMap<>();
        map.put("time", getCurrDate("yyyy-MM-dd HH:mm:ss"));
        map.put("userId", MyApplication.manager.getId());
        map.put("btnName", eventId);
        MobclickAgent.onEvent(MyApplication.getContext(), eventId, map);
    }

    public static void setCountEventPage(String eventId, String page) {
        Map<String, String> map = new TreeMap<>();
        map.put("time", getCurrDate("yyyy-MM-dd HH:mm:ss"));
        map.put("userId", MyApplication.manager.getId());
        map.put("pageName", eventId);
        map.put("source", page);
        MobclickAgent.onEvent(MyApplication.getContext(), eventId, map);
    }

    /**
     * 获取当前时间的指定格式
     */
    private static String getCurrDate(String format) {
        return dateToString(new Date(), format);
    }

    /**
     * 把日期转换为字符串
     */
    private static String dateToString(Date date, String format) {
        String result = "";
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            result = formater.format(date);
        } catch (Exception e) {
            // log.error(e);
        }
        return result;
    }
}
